package com.zzhrtech.wgzx_cityzen.adapters.contact;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.base.OnItemClickLitener;
import com.zzhrtech.wgzx_cityzen.beans.contact.ActivListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by renxiangpeng on 2017/2/23.
 */

public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<NormalRecyclerViewAdapter.NormalTextViewHolder> {
    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private List<ActivListBean.Data> beanList;
    private OnItemClickLitener mOnItemClickLitener;

    public NormalRecyclerViewAdapter(Context context) {
        mContext = context;
        beanList = new ArrayList<ActivListBean.Data>();
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NormalTextViewHolder(mLayoutInflater.inflate(R.layout.item_list_activ, parent, false));
    }

    @Override
    public void onBindViewHolder(NormalTextViewHolder holder, int position) {
        holder.tv_name.setText(beanList.get(position).getName());
        if (beanList.get(position).getIsend() || !beanList.get(position).getCanplay())  {
            holder.tv_mengview.setVisibility(View.VISIBLE);
            holder.tv_button.setBackgroundResource(R.drawable.shape_activoff);
            holder.tv_button.setTextColor(mContext.getResources().getColor(R.color.blackblack));
        }else {
            holder.tv_mengview.setVisibility(View.GONE);
            holder.tv_button.setBackgroundResource(R.drawable.shape_activon);
        }
        holder.tv_button.setText(beanList.get(position).getStringflag());
        holder.tv_button.setTextColor(mContext.getResources().getColor(R.color.white));
        holder.tv_time.setText(beanList.get(position).getStringtime());


        Glide.with(mContext)
                .load(beanList.get(position).getImgTh())
                .override(WgzxApplication.getInstance().getwidth(),WgzxApplication.getInstance().getwidth() * 260 / 690)
                .fitCenter()
                .placeholder(R.mipmap.ic_placehoder)
                .into(holder.iv_img);

        holder.position = position;
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    //添加列表
    public void addItem(ActivListBean.Data bean){
        beanList.add(bean);
        notifyDataSetChanged();
    }
    //清空列表
    public void cleanList(){
        beanList.clear();
    }

    public class NormalTextViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_img;
        TextView tv_mengview;
        TextView tv_button;
        TextView tv_time;
        int position;
        NormalTextViewHolder(View view) {
            super(view);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            iv_img = (ImageView) view.findViewById(R.id.iv_img);
            tv_mengview = (TextView) view.findViewById(R.id.tv_mengview);
            tv_button = (TextView) view.findViewById(R.id.tv_button);
            tv_time = (TextView) view.findViewById(R.id.tv_time);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickLitener != null){
                        mOnItemClickLitener.onItemClick(v,position);
                    }
                }
            });
        }
    }


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}